#include <stdio.h>
#include "STM32F4xx.h"
#include "LED.h"
#include "usb_bsp.h"
#include"usb_core.h"
#include "usbh_core.h"
#include "usbh_msc_core.h"
#include "usbh_usr.h"
#include"stm32f4xx_conf.h"
//USART_InitTypeDef USART_InitStructure;
//GPIO_InitTypeDef GPIO_InitStructure;
#include "stm32f4xx_usart.h"

USB_OTG_CORE_HANDLE USB_OTG_Core;
USBH_HOST USB_Host;

USBH_Class_cb_TypeDef USBH_MSC_cb;

volatile uint32_t msTicks;

void setup() {
	SystemCoreClockUpdate();

}
void USART_puts(USART_TypeDef *USARTx, volatile char *s) {
	while (*s) {
		while (!(USARTx->SR && 0x00000040))
			;
		USART_SendData(USARTx, *s);
		*s++;

	}
}
int main(void) {

	GPIO_InitTypeDef GPIO_InitStructure;
	/* STM32 evalboard user initialization */
	BSP_Init();
	USART_InitTypeDef USART_InitStructure;
	USBH_Init(&USB_OTG_Core, USB_OTG_FS_CORE_ID, &USB_Host, &USBH_MSC_cb,
			&USR_Callbacks);
	SystemCoreClockUpdate();

	if (SysTick_Config(SystemCoreClock / 1000)) { /* SysTick 1 msec interrupts  */
		while (1)
			; /* Capture error              */
	}

	LED_Init();
//	BTN_Init();
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	USART_InitStructure.USART_BaudRate = 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl =
			USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//	USART_Init()
	USART_Init(USART2, &USART_InitStructure);
	USART_Cmd(USART2, ENABLE);
//	printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");
//char str[] = "@Input your character: ";
//int i = 0;

//	USART_puts(USART2,"\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");

	while (1) {
		USBH_Process(&USB_OTG_Core, &USB_Host);
//		LED_On(1);
//		LED_On(2);
//		LED_On(3);
//		USART_SendData();
		//	//		; //xem thanh ghi co rong hok
//		printf("\n\rUSART Printf Example: retarget the C library printf function to the USART\n\r");
	}
	return 0;

}
